{include file="public/layout" /}

<body class="eyou_body_xin">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="system/left" /}
<div id="geduan_div" class="h15"></div>
<div class="page" style="min-width:auto;margin-left:115px;padding-bottom: 15px;">
    <!-- <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href="javascript:history.back();"  title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>广告配置</h3>
                <h5></h5>
            </div>
        </div>
    </div> -->
    {volist name="$advList" key="i" id="adv"}
    <div class="flexigrid wx_adv_table {gt name="$i" value="1"}mt50 pb30{/gt}" id="ad_position_{$adv.id}">
        <div class="hDiv">
            <div class="hDivBox">
                <table cellspacing="0" cellpadding="0" style="width: 100%">
                    <thead>
                        <tr>
                            <th abbr="" axis="col3">
                                <div class="tc" class="w40"></div>
                            </th>
                            <th abbr="" axis="col3">
                                <div class="tl text-l10" style="width: 100%">{$adv.title}</div>
                            </th>
                            <th abbr="" axis="col3" class="w120">
                                <div class="tc"></div>
                            </th>
                            <th abbr="" axis="col6" class="w80">
                                <div class="tc"></div>
                            </th>
                            <th abbr="" axis="col6" class="w60">
                                <div class="tc"></div>
                            </th>
                            
                        </tr>
                    </thead>
                </table>
            </div>
        </div>

        <div class="bDiv" style="height: auto;">
            <div class="adv-row-con">
                {volist name="$adv.list" key="j" id="ad"}
                <div class="adv-col-5" id="ad_{$ad.id}">
                    <div class="adv-etm">
                        <div class="adv-etm-l">
                            <img id="img_litpic" src="{$ad.litpic|get_default_pic=###}">
                            <span class="s-tihuan"><i class="iconfont e-bianji2 f-24" onclick="$('input[name=ad_id]').val({$ad.id});GetUploadify(1,'','allimg','edit_call_back');"></i></span>
                        </div>
                        <div class="adv-etm-r">
                            <div class="title">
                                <input name="title" value="{$ad.title}" placeholder="请输入标题" class="input-txt" type="text" onchange="changeTableVal('ad','id','{$ad.id}','title',this);" />
                            </div>
                            <div class="set-bt">
                                <input type="hidden" name="ad_link_id" value="{$ad.ad_link_id|default=0}">
                                <input type="hidden" name="ad_host_id" value="{$ad.ad_host_id|default=0}">
                                <span id="ad_link_title">{$ad.link_names|default=''}</span>
                                <a href="javascript:void(0);" data-ad_id="{$ad['id']}" data-ad_host_id="{$ad['ad_host_id']}" data-ad_link_id="{$ad['ad_link_id']}" onclick="selectLink(this);">设置链接</a>
                            </div>
                        </div>
                        <span class="delect" data-url="{:url('Other/del')}" data-id="{$ad.id}" onClick="delfun(this);"></span>
                    </div>
                </div>
                {/volist}
                <div class="adv-col-5" id="uploads_adv">
                    <div class="adv-etm">
                        <div class="adv-etm-l">
                            <a href="javascript:void(0);" onclick="$('input[name=pid]').val({$adv.id});GetUploadify(30,'','allimg','add_call_back');" class="adv-img-upload mb15" title="点击上传"> <div class="y-line"></div> <div class="x-line"></div> </a>
                        </div>
                        <div class="adv-etm-r">
                            <div class="title">
                                <input name="title" value="" placeholder="请输入标题" class="input-txt" type="text" />
                            </div>
                            <div class="set-bt">
                                <input type="hidden" name="ad_link_id" value="">
                                <input type="hidden" name="ad_host_id" value="">
                                <span id="ad_link_title"></span>
                                <a href="javascript:void(0);" data-ad_id="0" data-ad_host_id="0" data-ad_link_id="0" onclick="selectLink(this);">设置链接</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {gt name="$i" value="1"}
            <div class="iDiv" style="display: none;"></div>
            {/gt}
        </div>
    </div>
    {/volist}
    <input type="hidden" name="pid" value="0">
    <input type="hidden" name="ad_id" value="0">
</div>

<script type="text/javascript">

    // 添加广告回调函数
    function add_call_back(paths){
        var pid = $('input[name=pid]').val();
        if (pid <= 0) {
            showErrorAlert('保存失败');
            return false;
        }
        var title = $('#ad_position_'+pid+' #uploads_adv').find('input[name=title]').val();
        var ad_link_id = $('#ad_position_'+pid+' #uploads_adv').find('input[name=ad_link_id]').val();
        var ad_host_id = $('#ad_position_'+pid+' #uploads_adv').find('input[name=ad_host_id]').val();
        $.ajax({
            type : 'post',
            url : "{:url('Other/m_add', ['_ajax'=>1])}",
            data : {litpic_list:paths, pid:pid, title:title, ad_link_id:ad_link_id, ad_host_id:ad_host_id},
            dataType : 'json',
            success : function(res){
                if(res.code == 1){
                    var html = '';
                    for (var i = 0; i < res.data.list.length; i++) {
                        html += '<div class="adv-col-5" id="ad_'+res.data.list[i]['id']+'">';
                        html += '    <div class="adv-etm">';
                        html += '        <div class="adv-etm-l">';
                        html += '            <img id="img_litpic" src="'+res.data.list[i]['litpic']+'">';
                        html += '            <span class="s-tihuan"><i class="iconfont e-bianji2 f-24"></i></span>';
                        html += '        </div>';
                        html += '        <div class="adv-etm-r">';
                        html += '            <div class="title">';
                        html += '                <input name="title" value="'+res.data.list[i]['title']+'" placeholder="请输入标题" class="input-txt" type="text" onchange="changeTableVal(\'ad\',\'id\',\''+res.data.list[i]['id']+'\',\'title\',this);" />';
                        html += '            </div>';
                        html += '            <div class="set-bt">';
                        html += '                <input type="hidden" name="ad_link_id" value="'+res.data.list[i]['ad_link_id']+'">';
                        html += '                <input type="hidden" name="ad_host_id" value="'+res.data.list[i]['ad_host_id']+'">';
                        html += '                <span id="ad_link_title">'+res.data.list[i]['link_names']+'</span>';
                        html += '                <a href="javascript:void(0);" data-ad_id="'+res.data.list[i]['id']+'" data-ad_host_id="0" data-ad_link_id="'+res.data.list[i]['ad_link_id']+'" onclick="selectLink(this);">设置链接</a>';
                        html += '            </div>';
                        html += '        </div>';
                        html += '        <span class="delect" data-url="{:url('Other/del')}" data-id="'+res.data.list[i]['id']+'" onClick="delfun(this);"></span>';
                        html += '    </div>';
                        html += '</div>';
                    }
                    $('#ad_position_'+pid+' #uploads_adv').before(html);
                    $('#ad_position_'+pid+' #uploads_adv').find('input[name=title]').val('');
                    $('#ad_position_'+pid+' #uploads_adv').find('#ad_link_title').html('');
                    $('#ad_position_'+pid+' #uploads_adv').find('input[name=ad_link_id]').val(0);
                    $('#ad_position_'+pid+' #uploads_adv').find('input[name=ad_host_id]').val(0);
                    layer.msg(res.msg, {shade: layer_shade, time: 1000});
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                showErrorAlert(e.responseText);
            }
        });
    }

    // 编辑广告回调函数
    function edit_call_back(path){
        var ad_id = $('input[name=ad_id]').val();
        if (ad_id <= 0) {
            showErrorAlert('保存失败');
            return false;
        }
        var title = $('#ad_'+ad_id).find('input[name=title]').val();
        var ad_link_id = $('#ad_'+ad_id).find('input[name=ad_link_id]').val();
        var ad_host_id = $('#ad_'+ad_id).find('input[name=ad_host_id]').val();
        $.ajax({
            type : 'post',
            url : "{:url('Other/m_edit', ['_ajax'=>1])}",
            data : {litpic:path, ad_id:ad_id, title:title, ad_link_id:ad_link_id, ad_host_id:ad_host_id},
            dataType : 'json',
            success : function(res){
                if(res.code == 1){
                    $('#ad_'+ad_id+' #img_litpic').attr('src', path);
                    layer.msg(res.msg, {shade: layer_shade, time: 1000});
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                showErrorAlert(e.responseText);
            }
        });
    }

    function selectLink(obj) {
        var ad_id = $(obj).attr('data-ad_id');
        selectAdLink(obj, function(jsondata){
            var link_id = jsondata.link_id;
            var host_id = jsondata.host_id;
            var link_names = jsondata.link_names;
            if (0 < ad_id) {
                $.ajax({
                    type : 'post',
                    url : "{:url('Other/m_edit', ['_ajax'=>1])}",
                    data : {ad_id:ad_id, ad_link_id:link_id, ad_host_id:host_id},
                    dataType : 'json',
                    success : function(res){
                        if(res.code == 1){
                            $(obj).attr('data-ad_link_id', link_id);
                            $(obj).attr('data-ad_host_id', host_id);
                            $(obj).parent().find('#ad_link_title').html(link_names);
                            $(obj).parent().find('input[name=ad_link_id]').val(link_id);
                            $(obj).parent().find('input[name=ad_host_id]').val(host_id);
                            layer.msg(res.msg, {shade: layer_shade, time: 1000});
                        }else{
                            showErrorMsg(res.msg);
                        }
                    },
                    error: function(e){
                        showErrorAlert(e.responseText);
                    }
                });
            } else {
                $(obj).attr('data-ad_link_id', link_id);
                $(obj).attr('data-ad_host_id', host_id);
                $(obj).parent().find('#ad_link_title').html(link_names);
                $(obj).parent().find('input[name=ad_link_id]').val(link_id);
                $(obj).parent().find('input[name=ad_host_id]').val(host_id);
            }
        });
    }

    // 清空已选择的链接信息
    function clearLink() {
        $('#ad_link').val('');
        $('#ad_link_id').val(0);
        $('#ad_host_id').val(0);
    }
</script>

{include file="public/footer" /}